/*
 * 文件名：CrossingServiceImpl.java	 
 * 时     间：下午8:15:43
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.List;

import javax.tools.Tool;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cz0735.led.utils.CommondUtil;
import com.cz073586.tools.util.Tools;
import com.local.dao.mapers.CarParkInMapper;
import com.local.dao.mapers.CarParkOutMapper;
import com.local.dao.mapers.LocalScreenMapper;
import com.local.dao.mapers.ScreenDisplayMapper;
import com.local.model.CarParkOut;
import com.local.model.Screen;
import com.local.model.ScreenDisplay;
import com.local.model.ScreenExample;
import com.local.page.LocalScreenPage;
import com.local.service.face.LocalScreenService;
import com.local.service.face.LogService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.entity.PageInfo;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.UUIDGenerateUtil;

/**
 * 
 * 类描述：
 * 
 * @ClassName: LocalScreenServiceImpl
 * @author Administrator
 * @date 2016年4月25日 下午2:11:24
 */

@Service
public class LocalScreenServiceImpl extends
		BaseServiceImpl<Object, Object, String> implements LocalScreenService
{
	
	@Autowired
	private LocalScreenMapper localScreenMapper;
	
	@Autowired
	private ScreenDisplayMapper screenDisplayMapper;
	
	@Autowired
	private CarParkOutMapper carParkOutMapper;
	
	@Autowired
	private LogService logService;
	
	@Override
	public BaseMapper<Object, Object, String> getMapper()
	{
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void queryScreenByPage(LocalScreenPage<Screen> page)
	{
		
		try
		{
			Integer recordCount = localScreenMapper.countAllByPage(page);
			page.setTotalRecords(recordCount);
			List<Screen> list = localScreenMapper.queryByPage(page);
			
			page.setData(list);
			page.setPageSize(recordCount);
		} catch (Exception e)
		{
			throw new BaseException("", e);
		}
	}
	
	@Override
	public Integer addScreen(Screen screen) throws BaseException
	{
		try
		{
			if (Tools.isEmpty(screen.getName()))
			{
				throw new BaseException("50000-00011-00003");
			}
			
			List<Screen> list = getScreenByName(screen.getName());
			if (!list.isEmpty())
			{
				throw new BaseException("50000-00011-00004");
			}
			
			if (!Tools.isEmpty(screen)
					&& Tools.isEmpty(screen.getDefaultInfo()))
			{
				ScreenDisplay screenDisplay = screenDisplayMapper
						.queryScreenDisplay();
				if (!Tools.isEmpty(screenDisplay)
						&& !Tools.isEmpty(screenDisplay.getInfo()))
				{
					screen.setDefaultInfo(screenDisplay.getInfo());
				}
			}
			screen.setAttr1("1");
			screen.setUuid(UUIDGenerateUtil.generate());
			localScreenMapper.insertSelective(screen);
			
		} catch (Throwable t)
		{
			log.error(t.getMessage(), t);
			throw new BaseException(t.getMessage());
		}
		return null;
	}
	
	/**
	 * 
	 * 方法描述：根据名字查询
	 * 
	 * @Title: getScreenByName
	 * @param @param name
	 * @param @return
	 * @return List<Crossing>
	 * @user Administrator 2016年4月25日
	 * @throws
	 */
	private List<Screen> getScreenByName(String name)
	{
		ScreenExample condition = new ScreenExample();
		condition.or().andNameEqualTo(name);
		List<Screen> list = localScreenMapper.selectByCondition(condition);
		return list;
	}
	
	@Override
	public Integer delScreenById(String uuid) throws BaseException
	{
		try
		{
			localScreenMapper.deleteByPrimaryKey(uuid);
		} catch (Throwable t)
		{
			log.error(t.getMessage(), t);
			throw new BaseException(t.getMessage());
		}
		return null;
	}
	
	@Override
	public Integer editScreenById(Screen screen) throws BaseException
	{
		try
		{
			if (Tools.isEmpty(screen.getUuid()))
			{
				throw new BaseException("50000-00001-00001");
			}
			if (Tools.isEmpty(screen.getName()))
			{
				throw new BaseException("50000-00011-00003");
			}
			if (!localScreenMapper.selectByPrimaryKey(screen.getUuid())
					.getName().equalsIgnoreCase(screen.getName()))
			{
				List<Screen> list = getScreenByName(screen.getName());
				if (!list.isEmpty())
				{
					throw new BaseException("50000-00011-00004");
				}
			}
			localScreenMapper.updateByPrimaryKeySelective(screen);
		} catch (Throwable t)
		{
			log.error(t.getMessage(), t);
			throw new BaseException(t.getMessage());
		}
		return null;
	}
	
	@Override
	public Screen getScreenById(String uuid)
	{
		try
		{
			return localScreenMapper.selectByPrimaryKey(uuid);
		} catch (Throwable t)
		{
			log.error(t.getMessage(), t);
			throw new BaseException(t.getMessage());
		}
		
	}
	
	@Override
	public void saveOrUpdateScreenDisplay(ScreenDisplay screenDisplay)
	{
		try
		{
			List<ScreenDisplay> list = screenDisplayMapper
					.queryByPage(new PageInfo<ScreenDisplay>());
			if (!Tools.isEmpty(list) && !list.isEmpty())
			{
				list.get(0).setInfo(screenDisplay.getInfo());
				screenDisplayMapper.updateByPrimaryKey(list.get(0));
			} else
			{
				ScreenDisplay sd = new ScreenDisplay();
				sd.setUuid(UUIDGenerateUtil.generate());
				sd.setInfo(screenDisplay.getInfo());
				screenDisplayMapper.insertSelective(sd);
			}
		} catch (Throwable e)
		{
			throw new BaseException(e.getMessage());
		}
	}
	
	@Override
	public ScreenDisplay queryScreenDisplay()
	{
		ScreenDisplay screenDisplay = screenDisplayMapper.queryScreenDisplay();
		if (Tools.isEmpty(screenDisplay))
		{
			return null;
		} else
		{
			return screenDisplay;
		}
	}
	
	@Override
	public Screen queryScreenByCarOutUuid(String carOutUuid)
	{
		CarParkOut carParkOut = carParkOutMapper.selectByPrimaryKey(carOutUuid);
		if (!Tools.isEmpty(carParkOut))
		{
			return localScreenMapper.queryScreenByCamelDeviceId(carParkOut
					.getEquipment());
		}
		return null;
	}
	
	@Override
	public Integer restById(String uuid) throws BaseException
	{
		try
		{
			Screen screen = localScreenMapper.selectByPrimaryKey(uuid);
			if (!Tools.isEmpty(screen))
			{
				CommondUtil.reset(screen.getScreenIp(),
						Integer.parseInt(screen.getPortNo()),
						Integer.parseInt(screen.getScreenNo()));
			}
		} catch (Throwable t)
		{
			log.error(t.getMessage(), t);
			throw new BaseException(t.getMessage());
		}
		return null;
	}
	
}
